<?php 
/**
 * teacher管理学习情况的入口
 */
class Teacher_performance extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if (!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if ($this->session->userdata('r_role_id') != 2)
            redirect('home');
    }

    function index()
    {
        //准备数据
        $this->load->model('student_model');
        $this->load->model('teacher_model');
        $this->load->model('logic_class_model');
        $this->load->model('major_model');
        $this->load->model('module_model');
        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
        $school_id = $this->session->userdata('r_school_id');

        //配置查询选项

        $search_content = null;

        $not_limit_logic_class = array('id' => '0', 'name' => '--不限--');
        $logic_classes = array();
        array_push($logic_classes, $not_limit_logic_class);
        foreach ($this->logic_class_model->get_all_logic_classes_by_teacher_id($me['id']) as $logic_classes_item) {
            array_push($logic_classes, $logic_classes_item);
        }


        $performances = $this->student_model->get_performances($me['id'], $search_content);

        $data['performances'] = $performances;
        $data['logic_classes'] = $logic_classes;
        // $data['modules'] = $this->module_model->get_all_modules_by_teacher_id($me['id']);
        $data['modules'] = $this->module_model->get_modules_by_teacher_id($me['id']);
        $data['title'] = '学习情况';
        $this->load->view('teacher_performance/index', $data);

    }

    /**
     * 查询学生的表现
     * @return [type] [description]
     */
    function search()
    {
        $logic_class_id = $this->input->post('logic_class_select');
        $student_name = $this->input->post('student_name');
        $student_id = $this->input->post('student_id');
        $module_id = $this->input->post('module_id');
        $grades_low = $this->input->post('grades_low');
        $grades_high = $this->input->post('grades_high');
        $school_id = $this->session->userdata('r_school_id');

        // echo "logic_class_id";
        // var_dump($logic_class_id);
        // echo "major_id";
        // var_dump($major_id);

        $search_content = null;
        if ($logic_class_id)
            $search_content['logic_class_id'] = $logic_class_id;
        if ($student_name)
            $search_content['student_name'] = $student_name;
        if ($student_id)
            $search_content['student_id'] = $student_id;
        if ($module_id)
            $search_content['module_id'] = $module_id;
        if ($grades_low)
            $search_content['grades_low'] = $grades_low;
        if ($grades_high)
            $search_content['grades_high'] = $grades_high;

        //查询
        $this->load->model('teacher_model');
        $this->load->model('student_model');
        $this->load->model('logic_class_model');
        $this->load->model('module_model');
        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
        $performances = $this->student_model->get_performances($me['id'], $search_content);

        $not_limit_logic_class = array('id' => '0', 'name' => '--不限--');
        $logic_classes = array();
        array_push($logic_classes, $not_limit_logic_class);
        foreach ($this->logic_class_model->get_all_logic_classes_by_teacher_id($me['id']) as $logic_classes_item) {
            array_push($logic_classes, $logic_classes_item);
        }

        $data['performances'] = $performances;
        $data['logic_classes'] = $logic_classes;
        $data['modules'] = $this->module_model->get_all_modules_by_teacher_id($me['id']);

        $data['logic_class_id_selected'] = $logic_class_id;
        $data['student_name_selected'] = $student_name;
        $data['student_id_selected'] = $student_id;
        $data['module_id_selected'] = $module_id;
        $data['grades_low_selected'] = $grades_low;
        $data['grades_high_selected'] = $grades_high;

        $data['title'] = '学习情况';
        $this->load->view('teacher_performance/index', $data);

    }

    /**
     * 查看学生的试卷详情
     */
    function check_report()
    {
        $this->load->model('question_model');
        $this->load->model('report_model');
        $this->load->model('module_model');
        $this->load->model('student_model');
        $this->load->model('knowledge_point_model');


        $report_id = $this->uri->segment(3);

        $report_check = $this->report_model->get_by_id($report_id);
        $student = $this->student_model->get_by_id($report_check['student_id']);
        $module_check = $this->module_model->get_by_id($report_check['module_id']);

        $question_in_report_check = $this->report_model->get_from_question_in_report($report_id);


        $check_question = array();

        foreach ($question_in_report_check as $item) {


            $question_check = $this->question_model->get_by_id($item['question_id']);
            $knowledge_points_of_question = $this->knowledge_point_model->get_all_by_question_id($question_check['id']);
            //var_dump($knowledge_points_of_question);
            $each_check_question = array(
                "id" => $question_check['id'],
                "test_point_id" => $question_check['test_point_id'],
                "content_main" => $question_check['content_main'],
                "picture_main_id" => $question_check['picture_main_id'],
                "content_1" => $question_check['content_1'],
                "picture_1_id" => $question_check['picture_1_id'],
                "content_2" => $question_check['content_2'],
                "picture_2_id" => $question_check['picture_2_id'],
                "content_3" => $question_check['content_3'],
                "picture_3_id" => $question_check['picture_3_id'],
                "content_4" => $question_check['content_4'],
                "picture_4_id" => $question_check['picture_4_id'],
                "answer" => $question_check['answer'],
                "number" => $question_check['number'],
                "report_id" => $item['report_id'],
                "choose" => $item['choose'],
                "knowledge_points" => $knowledge_points_of_question
            );


            array_push($check_question, $each_check_question);
        }
        //var_dump($check_question);

        $data = array();
        $data['student'] = $student;
        $data['module'] = $module_check;
        $data['grades'] = $report_check['grades'];
        $data['check_question'] = $check_question;
        $data['title'] = "查看答卷详情";
        $this->load->view('teacher_performance/check_report', $data);
    }

    /**
     * 导出到excel
     * @return [type] [description]
     */
    function export_excel()
    {
        $logic_class_id = $this->input->post('logic_class_id');
        $student_name = $this->input->post('student_name');
        $class_id = $this->input->post('class_id');
        $student_id = $this->input->post('student_id');
        $major_id = $this->input->post('major_id');
        $module_id = $this->input->post('module_id');
        $grades_low = $this->input->post('grades_low');
        $grades_high = $this->input->post('grades_high');

        $search_content = null;
        if ($logic_class_id)
            $search_content['logic_class_id'] = $logic_class_id;
        if ($student_name)
            $search_content['student_name'] = $student_name;
        if ($student_id)
            $search_content['student_id'] = $student_id;
        if ($module_id)
            $search_content['module_id'] = $module_id;
        if ($grades_low)
            $search_content['grades_low'] = $grades_low;
        if ($grades_high)
            $search_content['grades_high'] = $grades_high;

        //查询
        $this->load->model('teacher_model');
        $this->load->model('student_model');
        $this->load->model('logic_class_model');
        $this->load->model('class_model');
        $this->load->model('major_model');
        $this->load->model('module_model');
        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));

        $query = $this->student_model->get_performances_query($me['id'], $search_content);
        // $this->load->helper('export_excel');
        // export_excel($query);
        $this->handle_excel($query);
    }

    /**
     * 将数据库的query导出到excel
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    function handle_excel($query)
    {
        if (!$query)
            return false;

        // Starting the PHPExcel library
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

      
        try {
            $objPHPExcel->setActiveSheetIndex(0);
        } catch (PHPExcel_Exception $e) {
        }

        // Field names in the first row
        $fields = $query->list_fields();
        $col = 0;
        foreach ($fields as $field) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
            $col++;
        }

        //设置逻辑班号的显示格式
        // $objPHPExcel->getActiveSheet()->getStyle('E')->getNumberFormat()->setFormatCode('0000');
        // $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
        
        // Fetching the table data
        $row = 2;
        foreach($query->result() as $data)
        {
            $col = 0;
            foreach ($fields as $field)
            {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
                $col++;
            }

            $row++;
        }

        try {
            $objPHPExcel->setActiveSheetIndex(0);
        } catch (PHPExcel_Exception $e) {
        }

        // foreach (range('A', $objPHPExcel->getActiveSheet()->getHighestDataColumn()) as $col) {
     //    $objPHPExcel->getActiveSheet()
     //            ->getColumnDimension($col)
     //            ->setAutoSize(true);
        // } 

        try {
            $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        } catch (PHPExcel_Reader_Exception $e) {
        }

        // Sending headers to force the user to download the file
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="学习情况.xls"');
        header('Cache-Control: max-age=0');

        $objWriter->save('php://output');
    }

    /**
     * 导出总成绩
     * @return [type] [description]
     */
    function export_all_grades()
    {
        //表单验证
        if ($this->form_validation->run('teacher_performance/export_all_grades')) {
            //post方法
            //读取数据
            $this->load->model('logic_class_model');
            $this->load->model('module_model');
            $this->load->model('student_model');
            $this->load->model('report_model');
            $logic_class_id = $this->input->post('logic_class_id');
            $logic_class = $this->logic_class_model->get_by_id($logic_class_id);
            if (!$logic_class)
                return $this->load->view('error');
            $relative_modules = $this->input->post('relative_modules');
            if (!$relative_modules) {
                $data['error_message'] = "您必须至少选择一个模块";
                return $this->load->view("error", $data);
            }
            
            //生成Excel
            $this->load->library('PHPExcel');
            $this->load->library('PHPExcel/IOFactory');

            $objPHPExcel = new PHPExcel();
            $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

            $objPHPExcel->setActiveSheetIndex(0);


            $students = $this->student_model->get_students_from_logic_class_id($logic_class_id);

            //设置表头的名称
            $col = 0;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, '姓名');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, '学号');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, '班级');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, '专业');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, '课程');
            foreach ($relative_modules as $module_id) {
                $module = $this->module_model->get_by_id($module_id);
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, $module['name']);
            }
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, '总分');

            //填入学生个人信息
            $row = 2;
            foreach ($students as $student) {
                $col = 0;
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $student['name']);
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $student['student_id']);
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $student['logic_class_name']);
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $student['major_name']);
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $student['big_lecture_name']);
                $row++;
            }

            //填入分数,同时计算总分
            $module_count = count($relative_modules);
            $row = 2;
            foreach ($students as $student) {
                $total_grades = 0;
                $col = 5;
                foreach ($relative_modules as $module_id) {
                    $grades = $this->report_model->get_grade($student['id'], $module_id, $logic_class['id']);
                    if (!$grades) $grades = 0;
                    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $grades);
                    $total_grades = $total_grades + $grades;
                }
                $total_grades = $total_grades / $module_count;
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $total_grades);
                $row++;
            }
            $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
            $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
            $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15);

            $objPHPExcel->setActiveSheetIndex(0);



            $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

            // Sending headers to force the user to download the file
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="总成绩' . $logic_class['name'] . '".xls');
            header('Cache-Control: max-age=0');

            $objWriter->save('php://output');

        }

        //get方法
        $this->load->model('teacher_model');
        $this->load->model('logic_class_model');
        $this->load->model('module_model');

        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
        $logic_classes = $this->logic_class_model->get_all_logic_classes_by_teacher_id($me['id']);
        $modules = $this->module_model->get_modules_by_teacher_id($me['id']);

        $data['logic_classes'] = $logic_classes;
        $data['modules'] = $modules;
        $data['title'] = '导出总成绩';
        $this->load->view('teacher_performance/export_all_grades', $data);
    }
}  
 